package org.cigo.fworld;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class DatabaseHelper extends SQLiteOpenHelper {
	
	private static String DB_PATH = "/data/data/org.cigo.fworld/databases/";
	private static String DB_NAME = "cigoDatabase";
	private SQLiteDatabase mDatabase;
	private final Context mContext;
	
	public DatabaseHelper(Context context){
		this(context, DB_NAME, null, 1);
	}
	
	/**
	 * @param context 上下文环境，如Activities
	 * @param name    数据库名称
	 * @param factory 游标工厂(可为Null) 
	 * @param version 版本
	 */
	public DatabaseHelper( Context context,  String name, 
			CursorFactory factory, int version) {
		super(context, name, factory, version);
		this.mContext = context;
	}

	public void createDatabase() throws IOException{
		boolean isDbExist = checkDatabase();
		if(!isDbExist){
			this.getWritableDatabase();
			try{
				copyDatabase();
			}catch(IOException ex){
				throw ex;
			}
		}
	}
	
	public void openDatabase() throws SQLiteException{
		String path = DB_PATH + DB_NAME;
		mDatabase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
	}
	
	private void copyDatabase() throws IOException{
		InputStream inStream = mContext.getAssets().open(DB_NAME);
		String outFileName = DB_PATH + DB_NAME;
		OutputStream outStream = new FileOutputStream(outFileName);
		//transfer bytes from the inputfile to the outputfile
		byte[] buffer = new byte[1024];
		int length = 0;
		while((length = inStream.read(buffer)) > 0){
			outStream.write(buffer, 0, length);
		}
		outStream.flush();
		outStream.close();
		inStream.close();
	}
	
	private boolean checkDatabase(){
		SQLiteDatabase db = null;
		try{
			String path = DB_PATH + DB_NAME;
			db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
		}catch(SQLiteException ex){
			
		}
		if(db!=null){
			db.close();
		}
		return (db != null);
	}
	
	// 执行SQL语句
	public void execSQL(String sql) throws SQLException, SQLiteException{
		if(mDatabase == null){
			openDatabase();
		}
		mDatabase.execSQL(sql);
	}
	
	public Cursor query(String sql){
		if(mDatabase == null){
			openDatabase();
		}
		return mDatabase.rawQuery(sql, null);
	}
	
	@Override
	public void onCreate(SQLiteDatabase arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}
	
	@Override
	public void onOpen(SQLiteDatabase db){
		super.onOpen(db);
	}
	
	@Override
	public synchronized void close(){
		if(mDatabase != null){
			mDatabase.close();
		}
		super.close();
	}
}
